/*
 * @Author: Crism
 * @Date: 2018-07-31 17:56:48
 * @LastEditors: Crism
 * @LastEditTime: 2018-08-09 14:59:17
 * @Description: 
 * @Email: crism@qq.com
 */
const HttpUtil = require('../util/HttpUtil');
const Utils = require('../util/Utils');
const cheerio = require('cheerio');

module.exports.getPoetics = getPoetics;

/* 过滤页面信息 */
async function getPoetics(poetic_url) {
    let url = Utils.getPoeticUrl(poetic_url);
    let totalPage = await getPoeticPageCount(url);

    for (let i = 1; i <= 1; i++) {
        url = Utils.getPoeticUrl(poetic_url, i);
        let html =await await HttpUtil.httpGet(url);
        if (html) {
            let $ = cheerio.load(html, { decodeEntities: false });
            let ListContent = $('div.left');

            let poetic = [];

            ListContent.find('div.cont').each(function (index, item) {
                let obj = $(this);
                poetic.push({
                    'title':    obj.find('p').find('a').find('b').text(),
                    'dynasty':  obj.find('p.source').find('a').first().text(),
                    'author':   obj.find('p.source').find('a').last().text(),
                    'poetic':   obj.find('div.contson').text(),
                    'url':   obj.find('p').find('a').attr('href').slice(1),
                })
            });
            return poetic;
        } else {
            console.log('无数据传入！');
        }
    }


}


async function getPoeticPageCount(poetic_url) {
    let html = await HttpUtil.httpGet(poetic_url);
    if (html) {
        let $ = cheerio.load(html, { decodeEntities: false });
        let ListContent = $('div.main3');
        return Utils.getPageCount(ListContent.find('div.title').find('h1').text());
    } else {
        return 0;
    }
}

